<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :label="$t('采购订单编号')" prop="purchaseOrderNumber">
        <el-input v-model="queryParams.purchaseOrderNumber" :placeholder="$t('通用_请输入') + $t('采购订单编号')" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item :label="$t('物料编码')" prop="partNumber">
        <el-input v-model="queryParams.partNumber"  :placeholder="$t('通用_请输入') + $t('物料编码')"  clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('库存批次号')"  prop="inventoryLotNumber">
        <el-input v-model="queryParams.inventoryLotNumber" :placeholder="$t('通用_请输入') + $t('库存批次号')" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t('通用_搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('通用_重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:instock:add']">{{ $t('通用_新增') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:instock:edit']">{{ $t('通用_修改') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:instock:remove']">{{ $t('通用_删除') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:instock:export']">{{ $t('通用_导出') }}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="instockList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('收货编号')"  align="center" prop="receiptNumber" />
      <el-table-column :label="$t('收货行号')" align="center" prop="receiptLineNumber" />
      <el-table-column :label="$t('采购订单编号')" align="center" prop="purchaseOrderNumber" />
      <el-table-column :label="$t('采购行号')" align="center" prop="purchaseLineNumber" />
      <el-table-column :label="$t('供应商')" align="center" prop="vendor" />
      <el-table-column :label="$t('物料编码')" align="center" prop="partNumber" />
      <el-table-column :label="$t('物料名称')" align="center" prop="materialName" />
      <el-table-column :label="$t('物料规格')" align="center" prop="materialSpecifications" />
      <el-table-column :label="$t('存储位置')" align="center" prop="storageLocation" />
      <el-table-column :label="$t('业务时间')" align="center" prop="dateBusiness" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateBusiness, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('实收数量')" align="center" prop="cuantityCollected" />
      <el-table-column :label="$t('收货批次号')" align="center" prop="receiptLotNumber" />
      <el-table-column :label="$t('库存批次号')" align="center" prop="inventoryLotNumber" />
      <el-table-column :label="$t('状态')" align="center" prop="state" />
      <el-table-column :label="$t('通用_操作')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:instock:edit']">{{ $t('通用_修改') }}</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:instock:remove']">{{ $t('通用_删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改材料入库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">


        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('收货编号')" prop="receiptNumber">
              <el-input v-model="form.receiptNumber" :placeholder="$t('通用_请输入') + $t('收货编号')"  />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('收货行号')" prop="receiptLineNumber">
              <el-input v-model="form.receiptLineNumber" :placeholder="$t('通用_请输入') + $t('收货行号')"   />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('采购订单编号')" prop="purchaseOrderNumber">
              <el-input v-model="form.purchaseOrderNumber" :placeholder="$t('通用_请输入') + $t('采购订单编号')"  />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('采购行号')" label="采购行号" prop="purchaseLineNumber">
              <el-input v-model="form.purchaseLineNumber" :placeholder="$t('通用_请输入') + $t('采购行号')" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('供应商')" prop="vendor">
              <el-input v-model="form.vendor" :placeholder="$t('通用_请输入') + $t('供应商')"  />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('物料编码')"  prop="partNumber">
              <el-input v-model="form.partNumber" :placeholder="$t('通用_请输入') + $t('物料编码')"  />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('物料名称')"   prop="materialName">
              <el-input v-model="form.materialName" :placeholder="$t('通用_请输入') + $t('物料名称')"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('物料规格')"   prop="materialSpecifications">
              <el-input v-model="form.materialSpecifications" :placeholder="$t('通用_请输入') + $t('物料规格')" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('存储位置')"   prop="storageLocation">
              <el-input v-model="form.storageLocation" :placeholder="$t('通用_请输入') + $t('存储位置')"  />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('业务时间')"  prop="dateBusiness">
              <el-date-picker clearable v-model="form.dateBusiness" type="date" value-format="yyyy-MM-dd"
              :placeholder="$t('通用_请输入') + $t('业务时间')" >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item  :label="$t('实收数量')"   prop="cuantityCollected">
              <el-input v-model="form.cuantityCollected"  :placeholder="$t('通用_请输入') + $t('实收数量')"  />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('收货批次号')"   prop="receiptLotNumber">
              <el-input v-model="form.receiptLotNumber" :placeholder="$t('通用_请输入') + $t('收货批次号')" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item  :label="$t('库存批次号')" prop="inventoryLotNumber">
              <el-input v-model="form.inventoryLotNumber" :placeholder="$t('通用_请输入') + $t('库存批次号')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item  :label="$t('状态')"  prop="inventoryLotNumber">
              <el-input v-model="form.state" :placeholder="$t('通用_请输入') + $t('状态')" />
            </el-form-item>
          </el-col>
        </el-row>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t('通用_确定') }}</el-button>
        <el-button @click="cancel">{{ $t('通用_取消') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listInstock,
    getInstock,
    delInstock,
    addInstock,
    updateInstock
  } from "@/api/system/instock";

  export default {
    name: "Instock",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 材料入库表格数据
        instockList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          groupId: null,
          receiptNumber: null,
          receiptLineNumber: null,
          purchaseOrderNumber: null,
          purchaseLineNumber: null,
          vendor: null,
          partNumber: null,
          materialName: null,
          materialSpecifications: null,
          storageLocation: null,
          dateBusiness: null,
          cuantityCollected: null,
          receiptLotNumber: null,
          inventoryLotNumber: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询材料入库列表 */
      getList() {
        this.loading = true;
        listInstock(this.queryParams).then(response => {
          this.instockList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          groupId: null,
          receiptNumber: null,
          receiptLineNumber: null,
          purchaseOrderNumber: null,
          purchaseLineNumber: null,
          vendor: null,
          partNumber: null,
          materialName: null,
          materialSpecifications: null,
          storageLocation: null,
          dateBusiness: null,
          cuantityCollected: null,
          receiptLotNumber: null,
          inventoryLotNumber: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加材料入库";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getInstock(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改材料入库";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateInstock(this.form).then(response => {
                this.$modal.msgSuccess(this.$t('通用_修改成功'));
                this.open = false;
                this.getList();
              });
            } else {
              addInstock(this.form).then(response => {
                this.$modal.msgSuccess(this.$t('通用_新增成功'));
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm(this.$t('通用_是否确认删除数据') + ids).then(function() {
          return delInstock(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t('通用_删除成功'));
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/instock/export', {
          ...this.queryParams
        }, `instock_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
